<script setup>
import { ref } from 'vue'

const x = ref('')
const y = ref('')
const result = ref('')

const calc = (o)=>{
  //eval("5+2")结果为7,会将字符串形式的算式转为JS进行运算!
  result.value = eval(x.value+o+y.value)
}
</script>

<template>
<h1>计算器练习</h1>
  <input type="text" v-model="x" placeholder="请输入数字1">
  <input type="text" v-model="y" placeholder="请输入数字2">
  <hr>
  <button @click="calc('+')">加</button>
  <button @click="calc('-')">减</button>
  <button @click="calc('*')">乘</button>
  <button @click="calc('/')">除</button>
  <h4>结果：{{result}}</h4>
</template>

<style scoped>

</style>